package com.community.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.community.pojo.Menu;
import com.community.pojo.MenuPermission;
import com.community.pojo.Permission;
import com.community.service.IMenuPermissionService;
import com.community.service.IMenuService;
import com.community.service.IPermissionService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 权限表 前端控制器
 * </p>
 *
 * @author TangHaoSong Fly ChenJie HeYuHang
 * @since 2024-05-15
 */
@CrossOrigin
@RestController
@RequestMapping("/community/permission")
public class PermissionController {

    @Autowired
    private IPermissionService permissionService;

    @Autowired
    private IMenuService menuService;

    @Autowired
    private IMenuPermissionService menuPermissionService;



    @RequestMapping("/updatePermissionStatus")
    public Object handleDelete(@RequestBody Permission permission) {
        permissionService.updateById(permission);
        return true;
    }

    @RequestMapping("/handleDelete/{id}")
    public Object handleDelete(@PathVariable Integer id) {
        Permission permission = permissionService.getById(id);
        if (permission != null) {
            permission.setIsDelete(1);
            permissionService.updateById(permission);
        }
        return true;
    }

    @RequestMapping("/savePerEditForm")
    public Object savePerEditForm(@RequestBody Permission permission) {
        boolean b = permissionService.updateById(permission);
        return b;
    }

    /**
     *
     * @param permission
     * @return
     */
    @RequestMapping("/saveNewPerForm/{menuId}")
    public Object saveNewPerForm(@PathVariable Integer menuId, @RequestBody Permission permission) {
        // 1. 添加一条新的权限
        boolean flag = permissionService.save(permission);

        if (flag) {
            // 获取新权限的 id
            Integer permissionId = permission.getId();

            // 2. 将新权限与菜单进行关联
            MenuPermission menuPermission = new MenuPermission();
            menuPermission.setMenuId(menuId);
            menuPermission.setPermissionId(permissionId);
            menuPermissionService.save(menuPermission);
        }

        return flag;
    }

    /**
     * 1.获取parent_id不为空的菜单,然后为每个菜单对应每个权限
     *
     * @return
     */
    @RequestMapping("/getMenusAndPer")
    public Object getMenusAndPer() {
        // 1. 创建 QueryWrapper 对象
        QueryWrapper<Menu> wrapper = new QueryWrapper<>();
        // 设置查询条件：parent_id 不为空
        wrapper.isNotNull("parent_id");
        wrapper.eq("is_delete",0);

        // 2. 使用 QueryWrapper 查询菜单列表
        List<Menu> menus = menuService.list(wrapper);

        // 3. 为每个菜单添加对应的权限列表
        for (Menu menu : menus) {
            List<Permission> permissions = menuService.getPermissionsByMenu1(menu);
            menu.setPermissions(permissions);
        }

        // 返回包含菜单和权限的数据结构
        return menus;
    }

}
